package com.centanet.framework.widget;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.centanet.centalib.R;


/**
 * 自定义加载view
 */
public abstract class AbstractLoadView extends RelativeLayout {

    protected RelativeLayout rl_custom_load;
    protected ImageView img_custom_load;
    protected ProgressBar pb_custom_load;
    protected AppCompatTextView atv_custom_load;
    protected View onlyChildView;

    private int loadStatus = -1;//加载状态[-1:初始化；0：加载成功；1：加载中；2：加载失败]
    private LoadStatusListener loadStatusListener;
    private LoadStatusListener listener = new LoadStatusListener() {
        @Override
        public void load() {
            if (loadStatusListener != null)
                loadStatusListener.load();
        }
    };

    public AbstractLoadView(Context context) {
        this(context, null);
    }

    public AbstractLoadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AbstractLoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_custom_load, this);
        rl_custom_load = (RelativeLayout) findViewById(R.id.rl_custom_load);
        img_custom_load = (ImageView) findViewById(R.id.img_custom_load);
        img_custom_load.setImageResource(getCustomLogo());
        pb_custom_load = (ProgressBar) findViewById(R.id.pb_custom_load);
        atv_custom_load = (AppCompatTextView) findViewById(R.id.atv_custom_load);

        img_custom_load.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loadStatus == 2) {
                    loadStatus = 1;
                    listener.load();
                    pb_custom_load.setVisibility(VISIBLE);
                    atv_custom_load.setVisibility(GONE);
                }
            }
        });
        atv_custom_load.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loadStatus == 2) {
                    loadStatus = 1;
                    listener.load();
                    pb_custom_load.setVisibility(VISIBLE);
                    atv_custom_load.setVisibility(GONE);
                }
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        if (count > 2)
            throw new IllegalStateException(" AbstractLoadView can host only one direct child");
        if (onlyChildView == null)
            onlyChildView = getChildAt(1);
        if (loadStatus == -1 &&
                onlyChildView != null &&
                loadStatusListener != null) {
            rl_custom_load.setVisibility(VISIBLE);
            onlyChildView.setVisibility(GONE);
        }
    }

    /**
     * 设置加载状态监听
     *
     * @param loadStatusListener {@link com.centanet.framework.widget.AbstractLoadView.LoadStatusListener}
     */
    public void setLoadStatusListener(LoadStatusListener loadStatusListener) {
        this.loadStatusListener = loadStatusListener;
    }

    /**
     * 加载成功
     */
    public void loadSuccess() {
        loadStatus = 0;
        if (onlyChildView != null) {
            onlyChildView.setVisibility(VISIBLE);
        }
        rl_custom_load.setVisibility(GONE);
        atv_custom_load.setVisibility(GONE);
    }

    /**
     * 加载出错
     */
    public void loadError() {
        loadStatus = 2;
        pb_custom_load.setVisibility(INVISIBLE);
        atv_custom_load.setVisibility(VISIBLE);
    }

    /**
     * 设置logo
     *
     * @return logo
     */
    @DrawableRes
    protected abstract int getCustomLogo();

    /**
     * 加载状态监听
     */
    public interface LoadStatusListener {
        void load();
    }
}
